<!-- 报警规则选择器 -->
<template>
  <div>
    <el-select v-model="currentRuleId" placeholder="请选择规则" @change="ruleChange" clearable>
          <el-option
            v-for="item in ruleList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
  </div>
</template>

<script setup lang="ts">
import { alarmsRuleList } from '@/api/vs/vacuumalarmsdevicerule'

const props = defineProps({
  ruleId: {
    type: Number,
    default: null
  }
})

watch(()=> props.ruleId,
() => {
  if(props.ruleId) {
    currentRuleId.value = Number(props.ruleId)
  }
})

const emit = defineEmits(['update:ruleId'])

const ruleChange = () => {
  emit('update:ruleId', currentRuleId.value)
}

onMounted(()=>{
  if(props.ruleId) {
    currentRuleId.value = props.ruleId
  }
})

// 当前规则id
const currentRuleId = ref()

// 规则列表数据
const ruleList = ref([])

// 加载规则列表
const loadAlarmsRuleList = async () => {
  try {
    const res = await alarmsRuleList()
    ruleList.value = res.map((item)=>{
      return {
        name: item.name,
        id: Number(item.id)
      }
    })
  } catch (error) {
    console.log(error)
  }
}

onMounted(loadAlarmsRuleList)
</script>

<style scoped>

</style>